//
//  AnalogClockController.m
//  iAnalogClock
//
//  Created by Le Thanh Hai on 10/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AnalogClockBlackController.h"


@implementation AnalogClockBlackController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Add background clock
    UIImageView *bgClock = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background_white.png"]];
    bgClock.backgroundColor = [UIColor clearColor];
    bgClock.frame = CGRectMake(0, 0, 184, 184);
    [self.view addSubview:bgClock];
    clock = [[AnalogClock alloc] initWithFrame:CGRectMake(0, 0, 184, 184) andR:0 G:0 B:0];
    clock.backgroundColor = [UIColor clearColor];
    [self.view addSubview:clock];
    [clock startClockUpdates];
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
	// Retrieve the touch point
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	startLocation = pt;
	[[self.view superview] bringSubviewToFront:self.view];
    
    if ([[touches anyObject] tapCount] > 1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Show" object: nil];
    }
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{    
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	CGRect frame = [self.view frame];
    float tempX = frame.origin.x + pt.x - startLocation.x;
    float tempY = frame.origin.y + pt.y - startLocation.y;
    if (tempX<0) {
        tempX = 0;
    }
    if (tempY<0) {
        tempY = 0;
    }
    NSLog(@"%f", [self.view superview].frame.size.width -self.view.frame.size.width );
    if (tempX>[self.view superview].frame.size.width -self.view.frame.size.width) {
        tempX = [self.view superview].frame.size.width -self.view.frame.size.width;
    }
    
    
    NSLog(@"%f", [self.view superview].frame.size.height -self.view.frame.size.height );
    if (tempY>[self.view superview].frame.size.height -self.view.frame.size.height) {
        tempY = [self.view superview].frame.size.height -self.view.frame.size.height;
    }
    
    frame.origin.x = tempX;
    frame.origin.y = tempY;
    
	[self.view setFrame:frame];
}

- (void)viewWillAppear:(BOOL)animated
{
    [clock startClockUpdates];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [clock stopClockUpdates];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
